package name.marz.to.go.ahg.saison.executer;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import name.marz.to.go.ahg.common.util.HttpUtil;
import name.marz.to.go.ahg.core.AutoGetterHttpClient;
import name.marz.to.go.ahg.core.Parameters;
import name.marz.to.go.ahg.core.checker.DocumentChecker;
import name.marz.to.go.ahg.core.executer.AbstractLoginExecuter;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.nodes.Document;

public class SaisonLoginExecuter extends AbstractLoginExecuter {
	private Document srcDoc;
	private DocumentChecker checker = new SaisonLoginChecker();

	public SaisonLoginExecuter(URI uri, AutoGetterHttpClient client, Document srcDoc) {
		this(uri, client,null,null,srcDoc);
	}

	public SaisonLoginExecuter(URI uri, AutoGetterHttpClient client,
			String[] userIds, String pass, Document srcDoc) {
		super(uri, client, userIds, pass);
		this.srcDoc = srcDoc;
		super.uri = HttpUtil.createURISafety(uri.getScheme()
				, uri.getAuthority()
				, HttpUtil.getFormActionString(srcDoc, "_USA01Form"));
	}

	@Override
	protected List<NameValuePair> getRequestParameters() {
		Parameters p = HttpUtil.getParametersFromInputTags(srcDoc);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("inputId", super.userIdentifiers[0]));
		params.add(new BasicNameValuePair("inputPassword", super.userPass));
		params.add(p.getNameValuePair("event"));
		params.add(p.getNameValuePair("inputIdCheckBox"));
		params.add(p.getNameValuePair("login"));
		params.add(p.getNameValuePair("org.apache.struts.taglib.html.TOKEN"));
		return params;
	}

	@Override
	protected DocumentChecker getChecker() {
		return checker;
	}
}